# 装饰器
# def log(func):
#     def wrapper(*args):
#         print(func.__name__)
#         func(*args)
#         print('log end')
#     return wrapper

# @log
# def funcNum(num):
#     print(num)

# funcNum(3)

# def log(txt):
#     def dec(func):
#         def wrapper(*args):
#             print(txt)
#             func(*args)
#             print('log end')
#         return wrapper
#     return dec

# @log('hello')
# def funcNum(num, num2):
#     print(num + num2)

# funcNum(4, 6)

# 计算函数执行时间
# import time

# # print(time.perf_counter())

# def metric(func):
#     def wrapper(*args):
#         timer = time.perf_counter()
#         result = func(*args)
#         timer = time.perf_counter() - timer
#         print('%s executed in %s ms' % (func.__name__, timer * 1000))
#         return result
#     return wrapper

# def fast(x, y):
#     time.sleep(0.0012)
#     return x + y

# # 使用函数式的写法
# fast = metric(fast)

# @metric
# def slow(x, y, z):
#     time.sleep(0.1234)
#     return x * y * z

# f = fast(11, 22)
# s = slow(11, 22, 33)

# print(f)
# print(s)

def log(arg):
    exeFun = None
    printStr = ''

    def wrapper(*args):
        print(printStr)
        exeFun(*args)

    # 接收字符串
    if isinstance(arg, str):
        def dec(func):
            nonlocal exeFun
            nonlocal printStr
            exeFun = func
            printStr = arg
            return wrapper
        return dec
    # 默认参数
    else:
        exeFun = arg
        printStr = 'Hello default'
        return wrapper

@log('kjhkjh')
# @log
def demo(txt):
    print(txt)

demo('Hello s')